﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MyOA.Model;


namespace MyOA.DAL
{
    public class PersonScheduleService
    {
        DBHelper db = new DBHelper();



        /// <summary>
        /// 根据登陆号获取登陆ID
        /// </summary>
        /// <param name="loginId">登陆号</param>
        /// <returns>获取登陆ID</returns>
        public int getUserIdByLoginId(string loginId)
        {
            int userId = -1;
            userId = (int)db.getScalar("sp_getUserIdByLoginId", new SqlParameter("@loginId", loginId));
            return userId;
        }

        /// <summary>
        /// 根据创建时间和创建人ID来获取第一个个人日程
        /// </summary>
        /// <param name="dt">创建时间</param>
        /// <param name="loginId">创建人ID</param>
        /// <returns>第一个个人日程</returns>
        public Schedule getScheduleByCreateTimeAndCreateUserId(DateTime dt, int loginId)
        {
            Schedule schedule = new Schedule();
            string createTime = dt.ToShortDateString();
            SqlParameter[] values = new SqlParameter[]
            {
                new SqlParameter("@createTime",createTime),
                new SqlParameter("@createUserId",loginId)
            };
            using (SqlDataReader reader = db.getDataReader("sp_getScheduleByCreateTimeAndCreateUserId", values))
            {
                if (reader.Read())
                {


                    schedule.ScheduleId = (int)reader[0];
                    schedule.Title = (string)reader[1];
                    schedule.Address = (string)reader[2];
                    schedule.BeginTime = (DateTime)reader[3];
                    schedule.EndTime = (DateTime)reader[4];
                    schedule.CreateTime = (DateTime)reader[5];
                    schedule.SchContent = (string)reader[6];
                    schedule.Meeting.MeetingId = (int)reader[7];
                    schedule.CreateUser.UserId = (int)reader[8];
                    schedule.CreateDepart.DepartId = (int)reader[9];
                }
            }
            db.CloseConnection();
            schedule.CreateUser.LoginId = getLoginIdByUserId(schedule.CreateUser.UserId);
            return schedule;
        }

        /// <summary>
        /// 根据登陆ID来获取登陆名
        /// </summary>
        /// <param name="userId">登陆ID</param>
        /// <returns>返回登陆名</returns>
        private string getLoginIdByUserId(int userId)
        {
            return (string)db.getScalar("sp_getLoginIdByUserId", new SqlParameter("@userId", userId));
        }
    }
}
